﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace NullSuicide
{
    class Item
    {
        private string _name;
        private int _ID;
        private string _wowheadURL;
        private string _htmlcode;
        
        private int _bossID;
        private string _bossName;
        private int _zoneID;
        private string _zoneName;

        public int BossID 
        { 
            get { return _bossID; }
            set { _bossID = value; }
        }

        public int ZoneID 
        { 
            get { return _zoneID; } 
            set { _zoneID = value; }
        }

        public string BossName
        {
            get { return _bossName; }
            set { _bossName = value; }
        }

        public string ZoneName
        {
            get { return _zoneName; }
            set { _zoneName = value; }
        }

        public Item(int ID, string name)
        {
            _ID = ID;
            _name = name;
            _wowheadURL = "http://www.wowhead.com/?item=" + ID;
            _htmlcode = "";
        }

        private string gethtmlFromWowhead()
        {
            int begin;
            int eind;
            string body;
            string htmlhead = "<LINK href='http://static.wowhead.com/css/global.css?260' rel='stylesheet' type='text/css'>";


            WebClient client = new WebClient();
            String htmlcode = client.DownloadString(_wowheadURL);
            htmlcode = htmlcode.Replace('\"', '\'');
            htmlcode = htmlcode.Replace("\\n", "");

            begin = htmlcode.IndexOf("<table><tr><td><b class=");
            eind =  htmlcode.IndexOf("<th style='background-position: bottom right'></th></tr></table>"); // eind van het item
            if (begin > 0 && eind > 0)
            {
                body = htmlcode.Substring(begin, eind - begin);

                _htmlcode = "<html><head>" + htmlhead + "</head><body>" + body + "</body></html>";
            }
            else
            {
                _htmlcode = "<html><head></head><body>Wowhead down for maintenance.</body></html>";
            }

            return _htmlcode;
        }

        public string URL 
        {
            get { return _wowheadURL; } 
        }

        public string HTML_BODY
        {
            get
            {
                if (_htmlcode.Equals(""))
                {
                     _htmlcode = gethtmlFromWowhead();
                }
                return _htmlcode;
            }
        }

        public int ID
        {
            get { return _ID; }
        }

        public string Name 
        {
            get { return _name; }
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
